package com.tz.crm.dao.impl;

import com.tz.crm.dao.CustomerDao;
import com.tz.crm.entity.Customer;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;

/**
 * 使用 @Repository 将 DAO 类声明为 Bean
 */
@Repository
@Transactional
@SuppressWarnings({"unchecked", "JpaQlInspection"})
public class CustomerDaoImpl extends BaseDaoImpl<Customer, String>
        implements CustomerDao {

    @Override
    public int getCountNewCustomer(int awayMonth) {
        String hql = "select count(id) from Customer where createDate between :myFromDate and :myToDate";
        Calendar firstDay = Calendar.getInstance();
        Calendar lastDay = Calendar.getInstance();
        firstDay.set(firstDay.get(Calendar.YEAR), firstDay.get(Calendar.MONTH)
                - awayMonth, 1);
        lastDay.set(lastDay.get(Calendar.YEAR), lastDay.get(Calendar.MONTH)
                - awayMonth + 1, 0);
        Query query = getSession().createQuery(hql);
        query.setParameter("myFromDate", firstDay.getTime());
        query.setParameter("myToDate", lastDay.getTime());
        return Integer.parseInt(query.uniqueResult().toString());
    }

    @Override
    public List<Customer> getAllSea() {
       String hql="from Customer where backSeaUserId is not null";
        Query query = getSession().createQuery(hql);
        return query.list();

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Customer> getByProvinceName(String provinceName) {
        String hql = "from Customer c where c.provinceName = :pname";
        return getSession().createQuery(hql)
                .setParameter("pname", provinceName)
                .list();
    }

    @Override
    public List<Customer> getByProvinceAndCityName(String provinceName,
                                                   String cityName) {
       return getSession().createQuery("from Customer where provinceName=:provinceName and cityName=:cityName")
                .setParameter("provinceName",provinceName)
                .setParameter("cityName",cityName)
                .list();

    }
}
